/*
 *The library i18nApplication is used for internationalizing the content of a page html.
 *
 *Use:
 *the engine uses the attribute data-i18n to retrive all the html elements that contains attribute or text to be tranlated.
 *Once all the elements of a page are retrived, the engine search any corresponding key in the dictionary, prior initialised with
 *the chosen language.
 *
 *Html attributes which can be tranlated:
 *-->value;
 *-->placeholder
 *-->title;
 *
 *Other content:
 *-->text; (direct child of the element)
 *
 *
 *EX: <a data-i18n="tran" placeholder="some other text">some text</a>
 *
 */



/*this function is called to makes the translation process.
 *It takes every html element marked with the attribute data-i18n
 *and pass it to the tranlating function.
 *Must be invoked after the page loads, it initialize the dictionary also.
 */
function initI18nEngine(lang){

  createDictionary(lang);
  $(":customdata(i18n)").each(function(){
    translatePage($(this));
  });
}

/*
 *A dictionary is an objects of a pair key->value that contains
 *all the words to be tranlated. It follows a special pattern
 *to identify the proper key that conteins the right word to be tranlated.
 *
 * the key is built by merging tree parts:
 * 1. the value assigned to the attribute data-i18n contained in the html element.
 * 2. a special separator 2x uderline __
 * 3. the name of the attribute/other content which the word translates: value|placeholder|title|text
 *
 *Initialize the dictionary with
 *@Param:lang; language identifier 
 **/
function createDictionary(lang){
 
  switch(lang){
    /*Italian*/
    case 'IT':
      initLangIT();
    break;
    /*English*/
    case 'EN':
      initLangEN();
    break;
    /*Germany*/
   case 'DE':
      initLangDE();
    break;
 /*
    case 'ES':
      initLangEN();
    break;

    case 'FR':
      initLangEN();
    break;*/

  }
}

/*Italian dictionary*/
function initLangIT(){
  /*application labels*/
  i18n_dict = {
  ity_dept_val__placeholder: 'partenza',
  ity_dept__value: 'cerca',
  ity_dept_show__text: 'indirizzi',
  ity_arr_val__placeholder: 'arrivo',
  ity_arr__value: 'cerca',
  ity_arr_show__text:'indirizzi',

   /*
    *
    *exception messages
    *
    **/
  ex_msg_001: 'Errore nella ricerca dell\'indirizzo: %s'

  };
   

  /*set dictionary*/
  $.i18n.setDictionary(i18n_dict);

}

/*English dictionary*/
function initLangEN(){

  /*application labels*/
  i18n_dict = {
  ity_dept_val__placeholder: 'departure',
  ity_dept__value: 'search',
  ity_dept_show__text: 'addresses',
  ity_arr_val__placeholder: 'arrival',
  ity_arr__value: 'search',
  ity_arr_show__text:'addresses',

   /*
    *
    *exception messages
    *
    **/
  ex_msg_001: 'Geocode was not successful for the following reason: %s'

  }

  /*set dictionary*/
  $.i18n.setDictionary(i18n_dict);
}

/*Germany dictionary*/
function initLangDE(){

  /*application labels*/
  i18n_dict = {
  cazzo__value: 'cazzo de value',
  cazzo__placeholder:'cazzo de place',
  fica__text: 'link fica de',
  culo__value: 'culo value de',
  fica_sub__value: 'fica sub button'


  };

  /*set dictionary*/
  $.i18n.setDictionary(i18n_dict);

}

/*
 *TRANSLATING FUNCTION
 **/
function translatePage(translatable){
  /**/
  var kPlaceHolder=   "__placeholder";
  var kValue=  "__value";
  var kText="__text";
 
  var i18nValue= $(translatable).customdata("i18n");
  var traslation;

  /*attribute placeholder*/
  if($(translatable).attr("placeholder")){
    /*search a reference in the current dictionary*/
    traslation= $.i18n._(i18nValue+kPlaceHolder);
    if(traslation){
      $(translatable).attr("placeholder", traslation);
    }
  }

  /*text*/
  if($(translatable).text()){
    traslation= $.i18n._(i18nValue+kText);
    if(traslation){
      $(translatable).text(traslation);
    }
  }
  
  /*attribute value*/
  if( $(translatable).val()){
   traslation= $.i18n._(i18nValue+kValue);
   if(traslation){
     $(translatable).val(traslation);
   }
  }
}